import { request } from '@umijs/max';
import { setAuthHeader } from '@/utils/authInterceptor';

/**
 * 添加物资外借记录
 * @param data 物资外借记录数据
 */
export async function addBorrowRecord(data: MATERIAL_BORROW.BorrowRecord) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/borrow/add', {
    method: 'POST',
    headers,
    data,
  });
}

/**
 * 更新物资外借记录
 * @param data 物资外借记录数据
 */
export async function updateBorrowRecord(data: MATERIAL_BORROW.BorrowRecord) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/borrow/update', {
    method: 'PUT',
    headers,
    data,
  });
}

/**
 * 删除物资外借记录
 * @param id 记录ID
 */
export async function deleteBorrowRecord(id: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/borrow/delete/${id}`, {
    method: 'DELETE',
    headers,
  });
}

/**
 * 根据ID获取物资外借记录
 * @param id 记录ID
 */
export async function getBorrowRecordById(id: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/borrow/get/${id}`, {
    method: 'GET',
    headers,
  });
}

/**
 * 分页查询物资外借记录
 * @param params 查询参数
 */
export async function getBorrowRecordPage(params: MATERIAL_BORROW.BorrowQueryParams) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/borrow/page', {
    method: 'POST',
    headers,
    data: params,
  });
}

/**
 * 归还物资
 * @param id 记录ID
 * @param isIntact 是否完好
 * @param remark 备注
 */
export async function returnBorrowRecord(id: number, isIntact: boolean, remark?: string) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/borrow/return/${id}?isIntact=${isIntact}${remark ? `&remark=${encodeURIComponent(remark)}` : ''}`, {
    method: 'PUT',
    headers,
  });
}